package cn.lu.train.ui.mine.activity;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;

import butterknife.BindView;
import butterknife.OnClick;
import cn.lu.train.App;
import cn.lu.train.R;
import cn.lu.train.base.BaseActivity;
import cn.lu.train.entry.ResultData;
import cn.lu.train.entry.User;
import cn.lu.train.mvp.presenter.user.ILoginPresenter;
import cn.lu.train.mvp.presenter.user.LoginPresenterImpl;
import cn.lu.train.mvp.view.ILoginView;
import cn.lu.train.ui.activity.MainActivity;
import cn.lu.train.util.LogUtils;
import cn.lu.train.util.SharePreferenceUtils;
import cn.lu.train.util.TextUtils;
import cn.lu.train.util.Toast;
import cn.lu.train.view.TitleView;

public class LoginActivity extends BaseActivity implements ILoginView{
    @BindView(R.id.title_view)
    TitleView mTitleView;
    @BindView(R.id.et_login_phone)
    EditText mEtPhone;
    @BindView(R.id.et_login_password)
    EditText mEtPassword;

    private ILoginPresenter loginPresenter;

    @Override
    public int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreated() {
        /*String s = "hello,565656";

        String key = "MIIDIzCCAgugAwIBAgIEQuuD6TANBgkqhkiG9w0BAQsFADBBMQswCQYDVQQGEwJaSDESMBAGA1UECBMJZ3Vhbmdkb25nMREwDwYDVQQHEwhzaGVuemhlbjELMAkGA1UEAxMCbHUwIBcNMTgwNTI2MDY0NjQ1WhgPMjE0MzA0MjYwNjQ2NDVaMEExCzAJBgNVBAYTAlpIMRIwEAYDVQQIEwlndWFuZ2RvbmcxETAPBgNVBAcTCHNoZW56aGVuMQswCQYDVQQDEwJsdTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKcuZKi2UsR3xRDJ6lRCTmQcZ+2abEJULsoWUR8r79mcHxofSDxnN3CEBc+OLHGPmsh8HDvA2BlAFm3C64Ohw2Umm0UqEr38WYqnEHFKnCgYFaKF7ORIg0Bu3MPtCbw5u5l2kRnhWbnU6ZrNY6ZA70y2xvphfGV4bYjnjv67TvKnkw9W2qmuW+ZfQ6YlTRJHVMDBd0HsmcCuL6oQO6khBMFf1xcAnMKbPwSNIDruJDd9TrxND92GNAdyr81rHmeX0ubTv1QeTAeeqtTE/vWIPgzpwHiu+hSlfa0oGE3jb2XDuUKXdI/qFfGqe2UHLEAS99/ga6djUpfFJvAaUyS3EWMCAwEAAaMhMB8wHQYDVR0OBBYEFGwPuq2abvVkI687+Wq2HvO/9bwDMA0GCSqGSIb3DQEBCwUAA4IBAQBdHoMC8NKyE2qHUDbdckw/0Oedwvh6yoGY5O7Jkycq8s716MYevHlXlsgTwcKWkocj7qSqxTP4J4FWFDBssMDIZ/Mg4hkKiRjSPWOmFvCvuf7B9WnW8C0uoak3n4h/33MJk5tww5wPXRJz5wVwHvh0EoX01UiSwov1Bb2F94ePg3cBZKYdbfM1kKlqjw3uq+VfsHyMVOEfts/BlSC4i1eRyrZDAtuku/GLF0qLj9eAYSPSxMYHT2JuJjKrBvC6WMg5nUjhIOewOdfmKdtlEdqCM8CnvFx5IGe7z0AXQ2h/niAuIBDfyBYil1xC9w7/hHiaP8TJK+z7KiTcogov+EXV";

        byte[] secretKey = {53, 79, 80, 2, 35, 79, -82, 63, -72, 120, 18, -58, 105, -42, -38, 123};
        String s1 = AESUtils.encrypt(s, secretKey);
        System.out.println("s1:" + s1);

        System.out.println("s2:"+AESUtils.decrypt(s1, secretKey));*/

        loginPresenter = new LoginPresenterImpl(this);
    }

    @OnClick({R.id.tv_login_login, R.id.tv_login_register, R.id.tv_login_forget, R.id.iv_login_qq, R.id.iv_login_weixin, R.id.iv_login_weibo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login_login:
                if (TextUtils.isEmpty(mEtPhone)) {
                    Toast.show("请输入账号/手机号");
                    return;
                }
                if (TextUtils.isEmpty(mEtPassword)) {
                    Toast.show("请输入密码");
                    return;
                }
               loginPresenter.login(mEtPhone.getText().toString(), mEtPassword.getText().toString());
                break;
            case R.id.tv_login_register:
                System.out.println(66957544);

                break;
            case R.id.tv_login_forget:
                break;
            case R.id.iv_login_qq:
                break;
            case R.id.iv_login_weixin:
                break;
            case R.id.iv_login_weibo:
                break;
        }
    }

    @Override
    public void onLoading() {
    }

    @Override
    public void onNext(ResultData<User> data) {
        if (data.success) {
            App.setUser(data.entry);
            SharePreferenceUtils.saveLoginState();
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            Toast.show(data.msg);
        }
    }

    @Override
    public void onError(Throwable e) {
        Toast.show(e.getMessage());
        LogUtils.e(e.getMessage());
    }

    @Override
    public void onComplete() {
        //Toast.show("oncomplete");
    }
}
